<!-- 直鱼管理 -->
<template>
  <BaseMainSlot>
    <template v-slot:searchbox>
      <el-collapse v-model="activeName" accordion @change="handleChange">
        <el-collapse-item name="1">
          <template slot="title">
            <div class="select-btns search-btns" @click.stop="changeSelect()">
              <i class="el-icon-circle-check"></i>
              <p v-if="activeName === '1'">收起筛选</p>
              <p v-if="activeName === ''">展开筛选</p>
            </div>
          </template>
          <div class="search-btn search-btns" @click.stop="queryPageList(), searchPage()">
            <i class="el-icon-circle-check"></i>
            <p>查询</p>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">鱼塘名称</div>
            <el-select filterable clearable v-model="fishPondId" filterable clearable @change="() => {
              productionBatchId = null;
            }">
              <el-option v-for="(item, index) in pondList" :key="index" :label="item.pondName" :value="item.fishPondId" />
            </el-select>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">批次编号</div>
            <el-select filterable clearable v-model="productionBatchId" filterable clearable>
              <template v-for="(item, index) in batchList">
                <el-option v-if="!fishPondId || item.fishPondId == fishPondId" :key="index"
                  :label="`${item.mnecode}_${item.varietyName}`" :value="item.productionBatchId" />
              </template>
            </el-select>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">处理人</div>
            <el-select filterable clearable v-model="staffName" placeholder="请选择" filterable clearable>
              <el-option v-for="(item, index) in staffList" :label="item.staffName" :value="item.staffName"
                :key="index + 'staff'" />
            </el-select>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">直鱼日期</div>
            <el-date-picker v-model="deadDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" clearable />
          </div>
        </el-collapse-item>
      </el-collapse>
    </template>
    <template v-slot:btnsBar>
      <ActionBar :export-active="true" :hasExport="false" :add-active="addActive" :del-active="delActive" :page-no="162"
        @add="add" @del="del" @exportFile="exportFile" :hasImportData="true" @getNewlistData="queryPageList"
        :importApi="'health/deadRecord/excelImport'" :upDownApi="'health/deadRecord/downloadTemplate'" />
    </template>
    <!-- <template v-slot:categoryBox>
      <CategoryBar :categoryList="categoryList" @categoryBar="categoryBar" />
    </template> -->
    <template v-slot:content>
      <el-table ref="tableData" v-loading="loading" highlight-current-row :data="tableData" stripe border
        height="calc(100% - 50px)" :row-key="row => {
          return row.deadRecordId;
        }
          " @selection-change="handleSelectionChange" @select="select" @select-all="selectAll">
        <el-table-column type="selection" width="55" :reserve-selection="true"
          :selectable="(row) => { return !row.selectable }" />
        <el-table-column type="index" label="序号" width="50">
          <template slot-scope="scoped">{{ scoped.row.deadRecordId ? scoped.$index + 1 : '' }}</template>
        </el-table-column>
        <el-table-column prop="pondName" label="鱼塘名称" />
        <el-table-column prop="mnecode" label="批次编号" />
        <el-table-column prop="productionBatchCode" label="批次编码" />
        <el-table-column prop="varietyName" label="直鱼品种" />
        <el-table-column prop="num" label="直鱼数量(尾)" />
        <el-table-column prop="weight" label="直鱼重量(斤)" />
        <el-table-column prop="staffName" label="处理人" />
        <el-table-column prop="deadDate" label="直鱼日期" />
        <el-table-column v-if="audit === true && success === ''" prop="deadDate" label="审核结果">
          <template v-if="scoped.row.deadRecordId" slot-scope="scoped">
            <div class="table-btns-box">
              <div v-if="scoped.row.auditType === 'PASS'" class="table-btns edit-btns">
                <p>通过审核</p>
              </div>
              <div v-if="scoped.row.auditType === 'NOT_PASS'" class="table-btns del-btns">
                <p>未通过审核</p>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column v-if="auditType !== 'PASS'" label="操作" width="200">
          <template v-if="scoped.row.deadRecordId" slot-scope="scoped">
            <div class="table-btns-box">
              <div v-if="scoped.row.auditType === 'REVIEW'" class="table-btns edit-btns" @click="edit(scoped.row)">
                <p>修改</p>
              </div>
              <div v-if="scoped.row.auditType === 'REVIEW'" class="table-btns edit-btns" @click="delOneData(scoped.row)">
                <p>删除</p>
              </div>
              <div v-if="scoped.row.auditType === 'NOT_PASS'" class="table-btns edit-btns"
                @click="showUnpassReason(scoped.row)">
                <p>查看未通过原因</p>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog :title="'未通过原因'" :visible.sync="showDialog" width="25%" top="0" :close-on-click-modal="false"
        @close="cancel">
        <el-form label-position="left" class="el-form">
          <el-form-item label="不通过原因: " prop="failReason" class="w100p">
            <el-input type="textarea" :rows="3" placeholder="请输入内容" disabled v-model="editObject.failReason"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="cancelbtn" @click="showDialog = false">取 消</el-button>
          <el-button type="primary" class="savebtn" @click="showDialog = false">保 存</el-button>
        </div>
      </el-dialog>
      <ExportBtn ref="ExportBtn" :btnName="'直鱼记录报表'" :apiName="'deadRecordExport'" :searchObj="searchObj">
      </ExportBtn>
      <Pagination :page-size="pageSize" :current-page="currentPage" :total-size="totalSize"
        @changePageSize="changePageSize" @changeCurrentPage="changeCurrentPage" />
      <AddEditDialog ref="addEditDialog" :now-chance-stau="nowChanceStau" :title-text="titleText"
        :edit-object="editObject" :show.sync="showAddEditDialog" @closeDialog="closeDialog" @cancelDialog="cancel" />
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
// import CategoryBar from "@/components/CategoryBar";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new.vue";
import AddEditDialog from "./components/add-edit-dialog.vue";
import ExportBtn from "@/components/ExportBtn";

import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";

import AbnormalDeathmanagementApi from "@/api/health-management/abnormal-death-management";
import StaffApi from "@/api/basic-info-manage/staff";
import EnumApi from "@/api/enum";
import BatchApi from "@/api/batch-mange/index";
import FishPondApi from "@/api/basic-info-manage/fishPond";
export default {
  name: "StaffManage",
  components: {
    ActionBar,
    Pagination,
    BaseMainSlot,
    AddEditDialog,
    // CategoryBar,
    ExportBtn
  },
  mixins: [mymixin],
  data () {
    return {
      searchObj: {},
      // 查询条件：单位名称
      staffName: "", // 处理人
      deadDate: "", // 直鱼日期
      productionBatchId: undefined, // 批次ID
      fishPondId: undefined, // 鱼塘ID
      audit: "",
      success: "",
      auditType: 'REVIEW', // 审核状态
      checkedIdList: [],
      staffList: [],
      batchList: undefined,
      pondList: undefined,
      categoryList: [],
      showDialog: false
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  created () {
    this.queryEnum();
    this.queryStaff();
    this.queryBatch();
    this.queryFishPond();
    this.queryPageList();
  },
  mounted () { },
  methods: {
    searchPage () {
      this.currentPage = 1
      this.queryPageList();
      setTimeout(() => {
        this.queryPageList();
      }, 200);
    },
    exportFile () {
      this.$refs['ExportBtn'].downloadFile()
      console.log(this.$refs['ExportBtn'])
    },
    queryBatch () {
      BatchApi.queryByCondition({}).then(res => {
        this.batchList = res.data
      })
    },
    queryFishPond () {
      FishPondApi.queryByCondition({}).then(res => {
        this.pondList = res.data;
      })
    },
    queryStaff () {
      StaffApi.queryByCondition().then(res => {
        this.staffList = res.data;
      });
    },
    queryEnum () {
      EnumApi.list({ enumType: "AuditType" }).then(res => {
        this.categoryList = []
        res.data.forEach((item, index) => {
          this.categoryList.push({
            id: index,
            name: item.value,
            value: item.name
          })
        });
      })
    },
    queryPageList () {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {}
      };
      if (this.auditType) params.entity.auditType = this.auditType;
      if (this.productionBatchId) params.entity.productionBatchId = this.productionBatchId;
      if (this.fishPondId) params.entity.fishPondId = this.fishPondId;
      if (this.staffName) params.entity.staffName = this.staffName;
      if (this.deadDate) params.entity.deadDate = this.deadDate;
      this.searchObj = JSON.parse(JSON.stringify(params.entity))
      AbnormalDeathmanagementApi.queryPageByCondition(params).then(res => {
        if (res.success === true) {
          let data = res.data || {};
          this.totalSize = data.totalElements;
          // 赋值数组
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    // 展示查看未通过原因弹窗
    showUnpassReason (row) {
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showDialog = true;
    },
    delOneData (row) {
      console.log(row)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.checkedIdList = [row.deadRecordId]
        this.delmessage()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 表格删除
    delmessage () {
      AbnormalDeathmanagementApi.delListById({ idList: this.checkedIdList }).then(res => {
        if (res.success) {
          this.$message.success("删除成功");
          this.queryPageList();
          this.resetActionBar();
        }
      });
    },
    changePageSize (pageSize) {
      this.pageSize = pageSize;
      this.queryPageList();
    },
    changeCurrentPage (currentPage) {
      this.currentPage = currentPage;
      this.queryPageList();
    },
    selectAll (list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.deadRecordId) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    select (list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.deadRecordId) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    handleSelectionChange (list) {
      this.checkedIdList = [];
      let notvalue = true;
      let emptyList = [];
      this.checkedIdList = list.map(item => {
        if (item.deadRecordId) {
          return item.deadRecordId;
        } else {
          notvalue = false;
          emptyList.push(item);
          return "";
        }
      });
      if (notvalue === false) {
        // this.$message.warning("不可选择空数据！");
        this.toggleNoSelection(emptyList);
        return;
      }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },
    // 添加
    add () {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      // this.addActive = false;
      // this.delActive = false;
      this.showAddEditDialog = true;
    },
    // 修改
    edit (row) {
      if (row.deadRecordId) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    }
  }
};
</script>

<style lang="scss" scoped></style>
